package com.our.sys.project1.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springside.modules.orm.hibernate.HibernateDao;

import com.our.sys.project1.pojo.PriceReview;

@Repository
public class QueryDao extends HibernateDao<Object, Long> {

    private JdbcTemplate jdbcTemplate;

    /**********************************价格审核*****************************************/
    RowMapper<PriceReview> prMapper = new RowMapper<PriceReview>() {

        @Override
        public PriceReview mapRow(ResultSet rs, int rowNum) throws SQLException {
            PriceReview pr = new PriceReview();
            pr.setDifferPrice(rs.getBigDecimal("differPrice"));
            pr.setDiscountDate(rs.getString("discountDate"));
            pr.setDiscountPrice(rs.getBigDecimal("discountPrice"));
            pr.setDiscountWho(rs.getString("discountWho"));
            pr.setFactoryCode(rs.getString("factoryCode"));
            pr.sethNumber(rs.getString("hNumber"));
            pr.setManagerCode(rs.getString("managerCode"));
            pr.setNumber(rs.getString("number"));
            pr.setPerson(rs.getString("person"));
            pr.setPrice(rs.getBigDecimal("price"));
            pr.setSupplier(rs.getString("supplier"));
            pr.setUseDate(rs.getString("useDate"));
            pr.setUseDepart(rs.getString("useDepart"));
            return pr;
        }
    };

    @Autowired
    public void setJdbcTemplate(@Qualifier("defaultDataSource") DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    /**
     * 价格审核
     * @param sql
     * @param list
     * @return
     * @throws Exception
     */
    public List<PriceReview> getPriceReview(String sql, List<Object> params) throws Exception {
        try {
            return jdbcTemplate.query(sql, params.toArray(), prMapper);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }
    
    
}
